//
//  DemoHUDVC.swift
//  BWUIKit_Example
//
//  Created by zhuxuhong on 2022/2/22.
//  Copyright © 2022 CocoaPods. All rights reserved.
//

import BWUIKit
import BWToolKit
import BWHUDKit

extension UIImage {
    static let success = UIImage(named: "success")!
    static let error = UIImage(named: "error")!
    static let warning = UIImage(named: "warning")!
}

class DemoHUDVC: ViewController {
    @IBAction func toast(_ sender: UIControl) {
        HUD.toast("This is a toast!")
    }
    
    @IBAction func tips(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            HUD.tips(.success, text: "Success")
        case 1:
            HUD.tips(.error, text: "Error")
        case 2:
            HUD.tips(.warning, text: "Warning")
        default: break
        }
    }
    
        
    @IBAction func loading(_ sender: UIControl) {
        HUD.loading(.ring, text: "I'm loading...(auto hide after 5s)", duration: 5)
    }
    
    var progress: CGFloat = 0
    @IBAction func progressing(_ sender: UIControl) {
        progress = 0
        GCD.asyncInGlobalQueue { [weak self] in
            guard let self = self else {
                return
            }
            while self.progress < 1 {
                self.progress += 0.02
                GCD.asyncInMainQueue {
                    HUD.progress(.pie, value: self.progress, text: String(format: "下载中: %d%%", Int(self.progress*100)))
                }
                usleep(50000)
            }
            GCD.asyncInMainQueue {
                HUD.dismiss()
                HUD.tips(.success, text: "下载成功")
            }
        }
    }
}
